package com.chance.cc.crawler.development.command.job.domain.weibo.subscribe;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpConfig;
import com.chance.cc.crawler.core.downloader.HttpConstant;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.downloader.HttpRequestBody;
import com.chance.cc.crawler.core.downloader.proxy.Proxy;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.development.command.job.CrawlerJobCommand;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import org.apache.commons.lang3.StringUtils;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.*;
import static com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime;

/**
 * @Author Zhao.Hhuan
 * @Date Create in 2021/1/26 16:25
 * @Description
 *      微博api用户订阅 每天凌晨回溯互动量
 **/
public class WeiboApiSubscribeUserIdInteractionCrawlerScheduleJob {
    private static final String domain = "weibo";

    private static final String source = "1428199813";

    public static void main(String[] args) {
//        publishApiSubscribeDataCrawlerScheduleJobInfo();
        System.out.println(publishJob().generateCrawlerTriggerKey());
    }

    public static Proxy proxy = new Proxy();
    static {
        //代理配置
        //H5168QRFNIU3804D
        //5F6B3610BB719FAA
        proxy.setHost("http-dyn.abuyun.com");
        proxy.setPort(9020);
        proxy.setUsername("HL89Q19E86E2987D");
        proxy.setPassword("71F33D94CE5F7BF2");
    }

    private static String service_ip = "192.168.1.215";
    private static int service_port = 9599;
    public static CrawlerJob publishApiSubscribeDataCrawlerScheduleJobInfo(){
        CrawlerJob weiboJob = publishJob();

        //发布单次任务
        CrawlerJobCommand crawlerJobCommand = new CrawlerJobCommand(service_ip,service_port);
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domain);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(weiboJob));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("微博订阅凌晨回溯一天内互动量");
        crawlerScheduleJob.setCrawlerKey(weiboJob.generateCrawlerKey());
        HttpPage page = crawlerJobCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布作业：" + page.getRawText());
        return weiboJob;

    }

    public static CrawlerJob publishJob(){
        String url = "https://c.api.weibo.com/2/statuses/count/biz.json";
        String site = "subscribeUserId";
            String siteBiz = "dingyue_userId-trace";
        HttpConfig httpConfig = HttpConfig.me(domain);
        httpConfig.setHttpSite(site);
        CrawlerRequestRecord crawlerRequestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domain, CrawlerEnum.CrawlerRequestType.turnPage)
                .domain(domain)
                .httpUrl(url)
                .httpConfig(httpConfig)
                .filter(CrawlerEnum.CrawlerRecordFilter.dateRange)
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(24 + 1,null))
                .needParsed(false)
                .releaseTime(System.currentTimeMillis())
                .resultLabelTag(CrawlerEnum.CrawlerDataType.interaction)
                .proxy(proxy)
                .build();
        crawlerRequestRecord.setDownload(false);
        crawlerRequestRecord.setSkipPipeline(true);
        crawlerRequestRecord.tagsCreator().bizTags().addDomain(domain);
        crawlerRequestRecord.tagsCreator().bizTags().addSite(site);
        crawlerRequestRecord.tagsCreator().bizTags().addSiteBiz(siteBiz);
        crawlerRequestRecord.getHttpRequest().addExtra("source",source);

        //获取地址的时候需要添加site确定拿取到的地址为本次需要的
        String requestQueueName = StringUtils.joinWith("-","crawler",domain,site,siteBiz,"queue");
        CrawlerRequestRecord initCrawlerRecord = CrawlerRequestRecord.builder()
                .startPageRequest("weibo_post_request",turnPageItem)
                .httpUrl("http://"+service_ip+":"+service_port+"/crawler/domain/common/api/v1/"+domain+"/results/queue/init?requestQueueName="+requestQueueName + "&site=dingyue_userId_post&hourFromNow=26")
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();
        HttpRequestBody jsonBody = HttpRequestBody.json(JSON.toJSONString(crawlerRequestRecord), "utf-8");
        initCrawlerRecord.getHttpRequest().setMethod(HttpConstant.Method.POST);
        initCrawlerRecord.getHttpRequest().setRequestBody(jsonBody);

        return CrawlerJob.builder()
                .triggerInfo(
                        domain,
                        CrawlerMetaConstant.ScheduleJobTrigger_Cron,
                        System.currentTimeMillis(),
                        StringUtils.joinWith("-",site,siteBiz,ScheduleJobTriggerJob_Realtime))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(requestQueueName))
//                .fileResultPipeline("kafka", "/data/chance_crawler_test/data/weibo/weiboSubscribeUserId.log", true)
                .kafkaResultPipeline("kafka","tmp_social_media_weibo",null)
                .requestRecord(crawlerRequestRecord)
                .supportRecord(initCrawlerRecord)
                .build();

    }
}
